  use Device::SerialPort qw( :PARAM :STAT 0.07 );
  $PortObj = new Device::SerialPort ("/dev/ttyUSB0", !$verbose) || undef $PortObj;
  $PortObj->read_const_time(200);       # const time for read (milliseconds)
  $PortObj->read_char_time(100);

  $PortObj->baudrate(19200);
  $PortObj->parity("none");
  $PortObj->parity_enable("FALSE");
  $PortObj->handshake("none");
  $PortObj->databits(8);
  $PortObj->stopbits(2);

  $PortObj->write_settings() || undef $PortObj;

#Get config file 
  my @lines;
		open DAT,"</var/www/temperature/temperature.dat";
		@lines = <DAT>;		# Read it into an array
		close(DAT);			# Close the file


  my $precise = true;
  my $c = 0;
  my $result;

#Inicjacja

  my $buffer = pack "C5", 0x55, 0x90, 0xac, 0x01, 0x03 ;
  my $this_count = $PortObj->write($buffer);
  my $flush = $PortObj->read($this_count);

  my $buffer = pack "C3", 0x53, 0x90, 0xee ;
  my $this_count = $PortObj->write($buffer);
  my $flush = $PortObj->read($this_count);

#Wait for the measurement process to be finished

while ($c < 128) {
  my $buffer = pack "C4", 0x55, 0x91, 0xac, 0x01 ;
  my $this_count = $PortObj->write($buffer);
  my $buffertxs = $PortObj->read(1);
  $c = unpack( "C" . length($buffertxs), $buffertxs);
}

  my $buffer = pack "C4", 0x55, 0x91, 0xaa, 0x02 ;
  my $this_count = $PortObj->write($buffer);

  my $buffert1 = $PortObj->read(1);
  my $buffert2 = $PortObj->read(1);

  my $a = unpack( "C" . length($buffert1), $buffert1);
  my $part = unpack( "C" . length($buffert2), $buffert2);
  if ($part == 128) {$b = "5";} else {$b = "0";} 
  if ($a >= 128) { 						#Temp below zero not supported yet
  $temp = -4;
  } else {
  $temp = $a;
  }

#counter:
  my $buffer = pack "C4", 0x55, 0x91, 0xa8, 0x01 ;
  my $this_count = $PortObj->write($buffer);
  my $buffert1 = $PortObj->read($this_count);
  $counter = unpack( "C" . length($buffert1), $buffert1);

#slope:
  my $buffer = pack "C4", 0x55, 0x91, 0xa9, 0x01 ;
  my $this_count = $PortObj->write($buffer);
  my $buffert1 = $PortObj->read($this_count);
  $slope = unpack( "C" . length($buffert1), $buffert1);

#temperature:
if ($precise) {
	if ($slope != 0) {
	$add = ($slope - $counter)/$slope;
	} else {
	$add  = 0.25;
	}
	$real_temp = $temp - 0.25 + $add;
	$result = sprintf "%2.2f", $real_temp . "\n";
} else {
$result = $a . "." . $b . "\n";
}
		open DAT,">/var/www/temperature/temperature";
		print DAT "$result\n$result\n";
		close DAT;
		print $result . "\n";

 	if ($result >= $lines[3]) {
		print "alarm 1 too high temp !\n";
  		send_notification_message();
	}
	if ($result <= $lines[4]) {
		print "alarm 2 too low temp !\n";
  		send_notification_message();
	}

sub send_notification_message {
		open DAT,"</var/www/temperature/temperature.lastalarm";
		@lastalarm = <DAT>;		# Read it into an array
		close(DAT);			# Close the file

		$curent_time = time();
		$time_passed = $curent_time - $lastalarm[0];

		if ($time_passed >= 900) {

		send_an_email();

		open DAT,">/var/www/temperature/temperature.lastalarm";
		print DAT "$curent_time\n";
		close(DAT);			# Close the file
		print "alarm email was send\n";
		} else {
		print "alarm email was not send!\n";
		}

}

sub send_an_email {

	$title='Perl Mail demo';
	$to='MAIL ADDRESS TO SEND TO';
	$from= 'webmaster@YOURDOMAIN.COM';
	$subject='YOUR SUBJECT';
 
	open(MAIL, "|/usr/sbin/sendmail -t");
 
	print MAIL "To: $lines[0]\n";
	print MAIL "From: Temperature monitor\n";
	print MAIL "Subject: $lines[1]\n\n";
	print MAIL "$lines[2] \n Current temperature: $result oC\n";
 	
	close(MAIL);
}

#dec2bin:
 # $out = unpack("B*", pack("N", $in));

